package com.exam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.exam.entity.User;
import com.exam.mapper.UserMapper;
import com.exam.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenzhuo
 * @since 2023-02-04 05:24:05
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public void getUserInfo(String username, String password, String secondPassword) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username",username);
        User user = userMapper.selectOne(queryWrapper);
        if (user != null){
            throw new RuntimeException("用户名已占用");
        }
        if (password == null || secondPassword == null || username == null ||password.equals("")||username.equals("")){
            throw new RuntimeException("用户名或密码不能为空");
        }
        char[] usernamechar = username.toCharArray();
        char[] passwordchar = password.toCharArray();
        System.out.println(usernamechar);
        for(int i=0;i<passwordchar.length;i++){
            if (passwordchar[i]==' '){
                throw new RuntimeException("密码不能有空格");
            }
        }
        for(int i=0;i<usernamechar.length;i++){
            if (usernamechar[i]==' '){
                throw new RuntimeException("用户名不能有空格");
            }
        }

        if (!password.equals(secondPassword)){
            throw new RuntimeException("两次密码输入不一致");
        }

        User user1 = new User();
        user1.setUsername(username);
        user1.setPassword(password);
        userMapper.insert(user1);

    }

    @Override
    public User getUserLoginInfo(String username, String password) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username",username);
        User user = userMapper.selectOne(queryWrapper);
        if (user == null){
            throw new RuntimeException("用户不存在");
        }
        if(!user.getPassword().equals(password)){
            throw new RuntimeException("密码错误");
        }
        return user;
    }
}
